<template>
	<div class="container add_edit">
		<el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px">
			<el-form-item label="标签名称" prop="name" ref="name">
				<el-input v-model="form.name" placeholder="请输入标签名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="图标" prop="img" ref="validImg">
				<uni-file-picker ref="img" v-model="imageValue" fileMediatype="image" returnType="object"
					:image-styles="imageStyle" @success="imgUpload" @delete="imgDelete" />
			</el-form-item>
			<el-form-item label="排序">
				<el-input type="number" v-model.number="form.sort" :min="1" placeholder="请输入排序" clearable
					oninput="value = parseInt(value) || 1"></el-input>
			</el-form-item>
			<el-form-item label="备注">
				<el-input v-model="form.remark" placeholder="请输入备注" clearable></el-input>
			</el-form-item>
			<el-form-item label="分类搜索">
				<el-switch v-model="form.is_search" active-color="#ff6a6c" inactive-color="#bbb" active-value="是"
					inactive-value="否"></el-switch>
			</el-form-item>
			<el-form-item label="状态">
				<el-switch v-model="form.state" active-color="#ff6a6c" inactive-color="#bbb" active-value="启用"
					inactive-value="禁用"></el-switch>
			</el-form-item>
			<el-form-item>
				<el-button :loading="formLoading" round icon="el-icon-check" class="confirm-btn"
					@click="submitForm('ruleForm')">提交</el-button>
				<el-button round icon="el-icon-back" class="return-btn" @click="returnPage">返回</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
	const __name = 'wmzmall-goods-tag';
	export default {
		data() {
			return {
				dataId: '',
				formLoading: false,
				form: {
					type: '商品',
					name: '',
					img: '',
					is_search: '是',
					sort: 0,
					remark: '',
					state: '启用'
				},
				imageValue: null,
				imageStyle: {
					height: '150px',
					width: '150px'
				},
				rules: {
					name: [{
						required: true,
						message: '请输入标签名称',
						trigger: 'change'
					}],
					img: [{
						required: true,
						message: '请选择图标',
						trigger: 'change'
					}]
				}
			};
		},
		methods: {
			async loadData() {
				if (!this.dataId) {
					this.$db[__name]
						.where({
							type: '商品'
						})
						.getsort()
						.then(res => {
							if (res.code == 200) {
								this.form.sort = ++res.datas.sort;
							}
						});
					return;
				}

				await this.$db[__name].tofirst(this.dataId).then(res => {
					if (res.code == 200) {
						for (let item in this.form) {
							this.form[item] = res.datas[item];
						}

						if (this.form.img) {
							this.imageValue = {};
							this.imageValue.url = this.form.img;
						}
					}
				});
			},
			submitForm(formName) {
				this.$refs[formName].validate((valid, obj) => {
					// 默认获取第一个未验证 form 属性名
					this.$api.set_unvalidated_form_focus(this, obj);
					if (valid) {
						this.formLoading = true;
						if (!this.dataId) {
							this.$db[__name].add(this.form).then(res => {
								if (res.code == 200) {
									this.getOpenerEventChannel().emit('loadData');
									this.returnPage();
								}
								this.formLoading = false;
							}).catch(() => {
								this.formLoading = false;
							})
						} else {
							this.$db[__name].update(this.dataId, this.form).then(res => {
								if (res.code == 200) {
									this.getOpenerEventChannel().emit('loadData');
									this.returnPage();
								}
								this.formLoading = false;
							}).catch(() => {
								this.formLoading = false;
							})
						}
					}
				});
			},
			imgUpload(e) {
				this.form.img = e.tempFilePaths[0];
				this.$refs.validImg.clearValidate();
			},
			imgDelete() {
				this.imageValue = null;
				this.form.img = '';
				this.$refs.ruleForm.validateField('img');
			},
			returnPage() {
				uni.$emit('closeTab');
				uni.redirectTo({
					url: '/pages/goods/label/page'
				});
			}
		},
		created() {
			this.dataId = this.$route.query.id || '';
			this.loadData();
		}
	};
</script>

<style></style>
